Table of Contents
Previous Section
You can write your own methods in WebScript. The methods you write can be associated with one of two types of objects: the WOApplication object that's automatically created when you run your script, or a WOWebScriptComponentController object that is associated with a particular grouping of a script, an HTML template, and a declarations file (for more information, see The Role of Scripts in a WebObjects Application). A WebObjects application has exactly one WOApplication object, but it can have multiple WOWebScriptComponentController objects. When you write your own methods, you're effectively extending the behavior of the object associated with the script.
You implement WOApplication methods in the application script. You implement WOWebScriptComponentController methods in a component script---that is, a script that has a corresponding HTML template and declarations file. This grouping of three files most commonly maps to a single, dynamically generated HTML page, but this isn't always the case---one component can be nested inside of another (for example, you could have a calendar component inside of a page).
To define a new method, simply put its implementation in the appropriate application or component script file. You don't need to declare it ahead of time. For example, the following method addFirstValue:toSecondValue: adds one value to another and returns the result:
- addFirstValue:firstValue toSecondValue:secondValue { id result; result = firstValue + secondValue; return result; }
In this example, note the following:
// This is fine. - aMethod:anArg { // NO!! This won't work. - (void) aMethod:(NSString *)anArg { // This won't work either. - (id)aMethod:(id)anArg {
To invoke the addFirstValue:toSecondValue: method shown above from another method, you'd simply do something like the following:
id sum, val1 = 2, val2 = 3; sum = [self addFirstValue:val1 toSecondValue:val2];